<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .w {
            width: 1226px;
            margin: 0 auto;
        }

        .product-intro {
            min-height: 500px;
            /* background-color: grey; */
            margin-top: 50px;
            display: flex;
        }

        .preview-wrap {
            width: 400px;
            position: relative;
        }

        .preview-wrap .preview {
            height: 400px;
            background-color: pink;
            position: relative;
        }

        .preview-wrap .preview .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.4);
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }

        .preview-wrap .preview img {
            width: 100%;
        }

        .preview-wrap .preview-scale {
            width: 400px;
            height: 400px;
            position: absolute;
            left: 420px;
            top: 0;
            /* background-color: #6ff; */
            background-color: rgba(125, 255, 255, 0.5);
            /* overflow: hidden; */
            display: none;
        }

        .preview-wrap .preview-scale img {
            width: 800px;
            height: 800px;
            position: absolute;
            left: 0;
            top: 0;
            z-index: -1;
        }

        .itemInfo-wrap {
            margin-left: 20px;
            flex: 1;
            /* background-color: orange; */
        }
    </style>
</head>

<body>
    <!-- magnifier -->
    <div class="product-intro w">
        <!--relative-->
        <div class="preview-wrap">
            <div class="preview">
                <img class="smallImg" src="../images/girlsmall1.jpg" alt="">
                <div class="shadow"></div>
            </div>
            <!-- <div class="preview-list">
                <ul>
                    <li><img src="" alt=""></li>
                    <li><img src="" alt=""></li>
                    <li><img src="" alt=""></li>
                    <li><img src="" alt=""></li>
                </ul>
            </div> -->
            <div class="preview-scale">
                <img class="bigImg" src="../images/girlbig1.jpg" alt="">
            </div>
        </div>
        <!--absolute-->
        <div class="itemInfo-wrap"></div>
    </div>
</body>
<script>
    /* 
    放大镜实现步骤
    1. 放大镜 => 呈比例关系放大  => previewScale/bigImg的布局
          shadow(200)          smallImg(400)        x
        ——————————————   =    ——————————————  = ———————— 
      previewScale(400)         bigImg(xxx)         X

    2. 左边移动的是 shadow  右边移动的是大图片  => bigImg的布局(定位)

    3. 移动的距离也是呈比例 且反向的
     
    */

    var previewWrap = document.getElementsByClassName("preview-wrap")[0];
    var preview = document.getElementsByClassName("preview")[0];
    var shadow = document.getElementsByClassName("shadow")[0];
    var previewScale = document.getElementsByClassName("preview-scale")[0];
    var smallImg = document.getElementsByClassName("smallImg")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];

    // 放大镜问题: 
    // 页面加载时  shadow,previewScale,bigImg默认隐藏  => clientWidth/clientHeight对于隐藏的元素不生效 => shadow.clientWidth shadow.clientHeight bigImg.clientWidth => 都是0 


    // ?? 
    var maxLeft = preview.clientWidth - shadow.clientWidth;
    var maxTop = preview.clientHeight - shadow.clientHeight;
    // console.log(maxLeft, preview.clientWidth, shadow.clientWidth);

    // ??
    var scale = bigImg.clientWidth / smallImg.clientWidth;


    preview.onmouseenter = function () {
        shadow.style.display = "block";
        previewScale.style.display = "block";
    }
    preview.onmousemove = function (e) {
        var e = e || window.event;

        var x = e.pageX - previewWrap.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - previewWrap.offsetTop - shadow.clientHeight / 2;

        if (x < 0) x = 0;
        if (x >= maxLeft) x = maxLeft;
        if (y < 0) y = 0;
        if (y >= maxTop) y = maxTop;

        // 取值时记得去单位,赋值时记得加单位
        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

        bigImg.style.left = -scale * x + "px";
        bigImg.style.top = -scale * y + "px";



    }

    preview.onmouseleave = function () {
        shadow.style.display = "none";
        previewScale.style.display = "none";
    }

</script>

</html>